package com.grep.extra.factory.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexUtil {
	
	private static String replaceSpecial(String str){
		str = str.replaceAll("'", "&#39;");
		return str ;
	}
	public static String getValueByRegex(String beginRegex, String endRegex, String orgStr){
		
		Pattern p1 = Pattern.compile(beginRegex);
		Pattern p2 = Pattern.compile(endRegex);
		Matcher m1 = p1.matcher(orgStr);
		Matcher m2 = p2.matcher(orgStr);
		while(m1.find()) { 
			int x = m1.end();
			while(m2.find()){
				int y = m2.start();
				if(x<y) {
					String s = orgStr.substring(x, y);
					if((!s.contains(beginRegex)) && (!s.contains(endRegex))){	
						if(s!=null){
							s = replaceSpecial(s);
						}
						return s;
					}
				}
			}
			m2 = p2.matcher(orgStr);
        }   
		return null;
	}
}
